package com.vote.admin.interceptor;

import com.vote.common.entity.VoteUser;
import com.vote.common.enums.AdminSessionEnum;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class ForceLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        VoteUser voteUser = (VoteUser) session.getAttribute(AdminSessionEnum.ADMIN.getCode());

        if(Objects.isNull(voteUser)){
            response.sendRedirect("login.html");
            return false;
        }

        return true;
    }
}
